import axios, { AxiosRequestConfig } from 'axios'

import { IRoute } from '../AddRouteModal/interface'
import { IPublishRouteCheckResultItem, IPublishRouteListItem, IPublishRouteListReq } from '../interface'
import './__mock__'

/** 获取发布路由列表 */
export const apiGetPublishRouteList = async (
  req: IPublishRouteListReq,
  signal?: AxiosRequestConfig['signal'],
): Promise<IPublishRouteListItem[]> => {
  return axios.post(`/tmsapi/route_publish/list`, req, { signal }).then(res => res.$data || [])
}

/** 删除发布路由 */
export const apiDelPublishRoute = async (publishId: number) => {
  await axios.post(`/tmsapi/route_publish/del`, { publishId })
}

/** 发布校验 返回null表示校验通过 */
export const apiPublishRouteCheck = async (req: {
  publishId: number
  occurDate: string
  keepDate: string
  routeFieldList?: Pick<IPublishRouteCheckResultItem, 'attrKey' | 'attrValue'>[]
}): Promise<IPublishRouteCheckResultItem[] | null> => {
  return axios.post(`/tmsapi/route_publish/validateRoute`, req).then(res => {
    const result = res.$data || []
    return result.length ? result : null
  })
}

/** 发布 */
export const apiPublishRoutePublish = async (req: {
  publishId: number
  occurDate: string
  keepDate: string
  fileList: NonNullable<IPublishRouteListItem['fileList']>
}) => {
  await axios.post(`/tmsapi/route_publish/publish`, req)
}

/** 获取运单下的路由列表 */
export const apiGetRouteList = async (waybillNo: string): Promise<IRoute[]> => {
  return axios.post(`/tmsapi/route_publish/listRouteTemplate`, { waybillNo }).then(res => res.$data)
}

/** 新增路由 */
export const apiAddRoute = async (waybillNo: string, routeIds: number[]) => {
  await axios.post(`/tmsapi/route_publish/add`, { waybillNo, routeIds })
}
